SHELL=/bin/bash
OS := $(shell uname -s)
######
CREDIS_HOME=third-party/credis-0.2.3
CREDIS=$(CREDIS_HOME)/credis.o
CREDIS_INC=-I$(CREDIS_HOME)
######
NDPI_INC=-I./nDPI/src/include
NDPI_LIB=./nDPI/src/lib/.libs/libndpi.a 
######
LIBPCAP=-lpcap
######
MONGOOSE_HOME=./third-party/mongoose
MONGOOSE_INC=-I$(MONGOOSE_HOME)
######
LUAJIT_HOME=./third-party/LuaJIT-2.0.2
LUAJIT_INC=-I$(LUAJIT_HOME)/src
LUAJIT_LIB=$(LUAJIT_HOME)/src/libluajit.a
######
LIBRRDTOOL_HOME=third-party/rrdtool-1.4.7
LIBRRDTOOL_INC=-I$(LIBRRDTOOL_HOME)/src/
LIBRRDTOOL_LIB=$(LIBRRDTOOL_HOME)/src/.libs/librrd_th.a
######
HTTPCLIENT_INC=third-party/http-client-c/src/
######
JSON_HOME=third-party/json-c
JSON_INC=$(JSON_HOME)
JSON_LIB=$(JSON_HOME)/.libs/libjson-c.a
######
ZEROMQ_HOME=./third-party/zeromq-3.2.3
ZEROMQ_INC=-I$(ZEROMQ_HOME)/include
ZEROMQ_LIB=$(ZEROMQ_HOME)/src/.libs/libzmq.a 
######
EWH_HOME=third-party/EWAHBoolArray
EWH_INC=$(EWH_HOME)/headers
######
TARGET = ntopng
LIBS = @LDFLAGS@ $(NDPI_LIB) $(LIBPCAP) $(LUAJIT_LIB) $(LIBRRDTOOL_LIB) $(ZEROMQ_LIB) $(JSON_LIB) -lxml2 @SQLITE_LIB@ @LINK_OPTS@ @LIBGNUTLS@ @GEOIP_LIB@ @Z_LIB@ -lm -ldl -lpthread
CPPFLAGS = -g @CFLAGS@ $(CREDIS_INC) $(MONGOOSE_INC) -I$(JSON_INC) $(NDPI_INC) $(LUAJIT_INC) $(LIBRRDTOOL_INC) $(ZEROMQ_INC) @CPPFLAGS@ -I$(HTTPCLIENT_INC) -I$(EWH_INC) # -D_GLIBCXX_DEBUG
######
# ntopng-1.0_1234.x86_64.rpm 
PLATFORM = `uname -p`
REVISION = @SVN_RELEASE@
PACKAGE_VERSION = @PACKAGE_VERSION@
RPM_PKG = $(TARGET)-$(PACKAGE_VERSION)-$(REVISION).$(PLATFORM).rpm
RPM_DATA_PKG = $(TARGET)-data-$(PACKAGE_VERSION)-$(REVISION).$(PLATFORM).rpm
######


ifeq ($(OS),Linux)
LIBS += -lrt
endif

.PHONY: default all clean

default: $(NDPI_LIB) $(LUAJIT_LIB) $(LIBRRDTOOL_LIB) $(ZEROMQ_LIB) $(JSON_LIB) $(TARGET)
all: default

OBJECTS = $(patsubst %.cpp, %.o, $(wildcard *.cpp)) $(CREDIS)
HEADERS = $(wildcard *.h)

%.o: %.c $(HEADERS) Makefile
	g++ $(CPPFLAGS) -c $< -o $@

%.o: %.cpp $(HEADERS) Makefile
	g++ $(CPPFLAGS) -c $< -o $@

.PRECIOUS: $(TARGET) $(OBJECTS)

$(TARGET): $(OBJECTS) $(LIBRRDTOOL) Makefile
	g++ $(OBJECTS) -Wall $(LIBS) -o $@

$(NDPI_LIB): nDPI
	cd nDPI; ./configure; @GMAKE@

nDPI:
	svn co https://svn.ntop.org/svn/ntop/trunk/nDPI/

$(LUAJIT_LIB):
	cd $(LUAJIT_HOME); @GMAKE@

$(ZEROMQ_LIB):
	cd $(ZEROMQ_HOME); ./configure --without-documentation; @GMAKE@

$(LIBRRDTOOL_LIB):
	cd $(LIBRRDTOOL_HOME); ./configure --disable-rrd_graph --disable-libdbi --disable-libwrap --disable-rrdcgi --disable-libtool-lock --disable-nls --disable-rpath --disable-perl --disable-ruby --disable-lua --disable-tcl --disable-python --disable-dependency-tracking; cd src; @GMAKE@ librrd_th.la

$(JSON_LIB):
	cd $(JSON_HOME); ./autogen.sh; ./configure; make

clean:
	-rm -f *.o *~ svn-commit.* #config.h
	-rm -f $(TARGET)
	cd $(CREDIS_HOME); make clean

veryclean:
	-rm -rf nDPI


geoip: httpdocs/geoip/GeoLiteCity.dat httpdocs/geoip/GeoLiteCityv6.dat httpdocs/geoip/GeoIPASNum.dat httpdocs/geoip/GeoIPASNumv6.dat

httpdocs/geoip/GeoLiteCity.dat:
	cd httpdocs/geoip; wget -nc http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz; gunzip -f GeoLiteCity.dat.gz

httpdocs/geoip/GeoLiteCityv6.dat:
	cd httpdocs/geoip; wget -nc http://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz; gunzip -f GeoLiteCityv6.dat.gz

httpdocs/geoip/GeoIPASNum.dat:
	cd httpdocs/geoip; wget -nc http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz; gunzip -f GeoIPASNum.dat.gz

httpdocs/geoip/GeoIPASNumv6.dat:
	cd httpdocs/geoip; wget -nc http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNumv6.dat.gz; gunzip -f GeoIPASNumv6.dat.gz

maketar:
	rm -rf ntopng-@PACKAGE_VERSION@
	mkdir ntopng-@PACKAGE_VERSION@
	cp -r *.cpp *.h configure COPYING README.* *.in ntopng.8 httpdocs scripts packages third-party ntopng-@PACKAGE_VERSION@
	find ntopng-@PACKAGE_VERSION@ -name ".svn" | xargs /bin/rm -rf
	cd ntopng-@PACKAGE_VERSION@; svn co https://svn.ntop.org/svn/ntop/trunk/nDPI/; cd ..
	tar cvfz ntopng-@PACKAGE_VERSION@.tgz ntopng-@PACKAGE_VERSION@
	#
	rm -rf ntopng-data-@PACKAGE_VERSION@
	mkdir -p ntopng-data-@PACKAGE_VERSION@/usr/local/share/ntopng/httpdocs/geoip/
	@if test -d ~/dat_files ; then \
	  cp ~/dat_files/* ntopng-data-@PACKAGE_VERSION@/usr/local/share/ntopng/httpdocs/geoip/; \
	else \
	  cp ./httpdocs/geoip/* ntopng-data-@PACKAGE_VERSION@/usr/local/share/ntopng/httpdocs/geoip/ ;\
	fi
	#gunzip ntopng-data-@PACKAGE_VERSION@/usr/local/share/ntopng/httpdocs/geoip/*.gz
	tar cvfz ntopng-data-@PACKAGE_VERSION@.tgz ntopng-data-@PACKAGE_VERSION@


build-rpm: maketar
# Do NOT build package as root (http://wiki.centos.org/HowTos/SetupRpmBuildEnvironment)
#	mkdir -p $(HOME)/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
#	echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros
	rm -f $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_PKG)
	cp ntopng-@PACKAGE_VERSION@.tgz $(HOME)/rpmbuild/SOURCES/
	cp ntopng-data-@PACKAGE_VERSION@.tgz $(HOME)/rpmbuild/SOURCES/
	#
	rpmbuild -bb ./packages/ntopng.spec --define "buildnumber $(REVISION)"
	@./packages/rpm-sign.exp $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_PKG)
	@echo ""
	@echo "Package contents:"
	@rpm -qpl $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_PKG)
	@echo "The package is now available in $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_PKG)"
	-rm -rf ntopng-@PACKAGE_VERSION@ ntopng_@PACKAGE_VERSION@_@SVN_RELEASE@.tgz
	#
	cd $(HOME)/rpmbuild/SOURCES;tar xvfz $(HOME)/rpmbuild/SOURCES/ntopng-data-@PACKAGE_VERSION@.tgz
	rpmbuild -bb ./packages/ntopng-data.spec --define "buildnumber $(REVISION)"
	@./packages/rpm-sign.exp $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_DATA_PKG)
	@echo ""
	@echo "Package contents:"
	@rpm -qpl $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_DATA_PKG)
	@echo "The package is now available in $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_DATA_PKG)"
	-rm -rf ntopng-data-@PACKAGE_VERSION@ ntopng-data_@PACKAGE_VERSION@_@SVN_RELEASE@.tgz


install: ntopng geoip
	@echo "While we provide you an install make target, we encourage you"
	@echo "to create a package and install that"
	@echo "rpm - do 'make build-rpm'"
	@echo "deb - do 'cd packages/ubuntu;./configure;make"
	mkdir -p /usr/local/share/ntopng /usr/local/man/man8 /usr/local/bin
	cp ntopng /usr/local/bin
	cp ./ntopng.8 /usr/local/man/man8
	cp -r ./httpdocs /usr/local/share/ntopng
	cp -r ./scripts /usr/local/share/ntopng
	find /usr/local/share/ntopng -name "*~"   | xargs /bin/rm -f
	find /usr/local/share/ntopng -name ".svn" | xargs /bin/rm -rf

Makefile: @SVN_WC_DB@
	@echo ""
	@echo "Re-running configure as the SVN release has changed"
	@echo ""
	./configure
